Skip to main content

Overview

This standalone script fetches all NSE indices (NIFTY 50, NIFTY BANK, NIFTY IT, etc.) with live market data including OHLC, LTP, volume, and 52-week highs/lows.

Why Standalone?

  • Different Data Structure: Indices have different fields compared to stocks
  • Separate Use Case: Used for market breadth analysis and index tracking
  • Independent Updates: May need to be run at different frequencies than stock data
  • Specialized Filtering: Requires specific parameter combinations (Inst: IDX, Exch: IDX)

What It Fetches

The script retrieves real-time and historical data for NSE indices:

Market Data Fields

  • Live Data: Open, High, Low, LTP (Last Traded Price)
  • Price Changes: Absolute change (Chng) and percentage change (PChng)
  • Volume: Today’s cumulative volume (Min1TotalVolPrevCandle)
  • 52-Week Range: 52-week high and low values

Metadata Fields

  • Symbol (e.g., NIFTY 50, NIFTY BANK)
  • Display Symbol
  • Index ID (Sid)
  • Exchange and Segment
  • Instrument Type

Output Files

all_indices_list.json
JSON file
Contains array of index objects with live market data. Saved in the current working directory.
Example Output Structure:
[
  {
    "IndexName": "NIFTY 50",
    "Symbol": "NIFTY 50",
    "IndexID": "12345",
    "Exchange": "IDX",
    "Segment": "IDX",
    "Instrument": "IDX",
    "Open": 21500.50,
    "High": 21650.75,
    "Low": 21480.25,
    "Ltp": 21625.00,
    "Chng": 125.50,
    "PChng": 0.58,
    "Volume": 245678900,
    "52W_High": 22000.00,
    "52W_Low": 18500.00
  }
]

API Reference

fetch_all_indices()

Fetches all NSE indices from the ScanX Pro API with live market data.
None
void
This function takes no parameters.
Returns: bool
  • True if fetch was successful
  • False if API error or critical failure
API Endpoint:
url = "https://ow-scanx-analytics.dhan.co/customscan/fetchdt"
Payload Configuration:
payload = {
    "data": {
        "sort": "Sym",
        "sorder": "asc",
        "count": 500,
        "fields": [
            "Sym", "DispSym", "Sid", "Exch", "Seg", "Inst", 
            "Open", "High", "Low", "Ltp", "Pchange", "PPerchange", 
            "High1Yr", "Low1Yr", "Min1TotalVolPrevCandle"  # Today's volume
        ],
        "params": [
            {"field": "Inst", "op": "", "val": "IDX"},
            {"field": "Exch", "op": "", "val": "IDX"}
        ],
        "pgno": 1
    }
}
Volume Handling: The script uses Min1TotalVolPrevCandle to capture today’s cumulative volume and handles negative overflow values:
vol = item.get('Min1TotalVolPrevCandle', 0)
if isinstance(vol, (int, float)) and vol < 0:
    vol = 0  # Reset negative overflow values
Error Handling:
  • 15-second timeout for API requests
  • Checks HTTP status code (expects 200)
  • Validates response structure
  • Prints informative error messages

When to Run Manually

Run during market hours to get live index movements and volume data.
Run after market close to capture final closing values for all indices.
When NSE launches a new index that you want to track.
Before running analysis scripts that need current index levels.

Usage

python3 fetch_all_indices.py
Expected Output:
Fetching NSE Indices & Today's Volume via ScanX Pro API...
Successfully identified 65 Indices with Today's Volume.

Source Code

import requests
import json
from pipeline_utils import get_headers

def fetch_all_indices():
    """
    Fetches strictly NSE Indices using ScanX Pro API.
    Captures Live LTP, OHLC, and TODAY'S VOLUME (from technical fields).
    """
    print("Fetching NSE Indices & Today's Volume via ScanX Pro API...")
    
    url = "https://ow-scanx-analytics.dhan.co/customscan/fetchdt"
    
    payload = {
        "data": {
            "sort": "Sym", "sorder": "asc", "count": 500,
            "fields": [
                "Sym", "DispSym", "Sid", "Exch", "Seg", "Inst", 
                "Open", "High", "Low", "Ltp", "Pchange", "PPerchange", 
                "High1Yr", "Low1Yr", "Min1TotalVolPrevCandle" # Cumulative Today Volume
            ],
            "params": [
                {"field": "Inst", "op": "", "val": "IDX"},
                {"field": "Exch", "op": "", "val": "IDX"}
            ],
            "pgno": 1
        }
    }

    try:
        response = requests.post(url, json=payload, headers=get_headers(), timeout=15)
        if response.status_code == 200:
            raw_data = response.json().get('data', [])
            
            final_indices = []
            for item in raw_data:
                # Get volume, ensure it is positive
                vol = item.get('Min1TotalVolPrevCandle', 0)
                if isinstance(vol, (int, float)) and vol < 0: 
                    vol = 0

                final_indices.append({
                    "IndexName": item.get('DispSym'),
                    "Symbol": item.get('Sym'),
                    "IndexID": item.get('Sid'),
                    "Exchange": item.get('Exch'),
                    "Segment": item.get('Seg'),
                    "Instrument": item.get('Inst'),
                    "Open": item.get('Open'),
                    "High": item.get('High'),
                    "Low": item.get('Low'),
                    "Ltp": item.get('Ltp'),
                    "Chng": item.get('Pchange'),
                    "PChng": item.get('PPerchange'),
                    "Volume": vol,  # TODAY'S LIVE VOLUME
                    "52W_High": item.get('High1Yr'),
                    "52W_Low": item.get('Low1Yr')
                })
            
            print(f"Successfully identified {len(final_indices)} Indices with Today's Volume.")
            
            output_file = "all_indices_list.json"
            with open(output_file, 'w') as f:
                json.dump(final_indices, f, indent=4)
            return True
        else:
            print(f"API Error: Status {response.status_code}")
            return False
    except Exception as e:
        print(f"Critical API Failure: {e}")
        return False

if __name__ == "__main__":
    fetch_all_indices()

Dependencies

  • requests: HTTP library for API calls
  • json: JSON serialization/deserialization
  • pipeline_utils: Shared utilities module
    • Uses get_headers() for randomized user-agent headers
This script uses get_headers() from pipeline_utils.py for better request handling with user-agent rotation.

Integration Notes

While this is a standalone script, the output can be used by:
  • Market breadth analysis tools
  • Index correlation studies
  • Sector rotation strategies
  • Portfolio benchmarking
The JSON output format is designed to be easily imported into other analysis scripts.